<?php

/**
 * ProductPicture form.
 *
 * @package    Web20
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ProductPictureForm extends BaseProductPictureForm
{
  public function configure()
  {
  	unset($this["created_at"], $this["updated_at"]);

	$this->setWidgets(array(
       	'id'	  	 	=> new sfWidgetFormInputHidden(),
      	'product_id'	=> new sfWidgetFormInputHidden(),
      	'description'	=> new sfWidgetFormInput(array('label' => "Descripción"), array('size' => 20)),
      	'file'			=> new sfWidgetFormInputFileEditable(
			array(
				'label'	   => "Archivo",
				'file_src' => $this->getObject()->getThumbnailFileName(),
				'is_image' => true,
				'delete_label' => "Eliminar",
				'template'  => '<div style:"float:left;">%file%</div>
						<div>%input%
						%delete% %delete_label%</div>
						',
			))

	));

    $this->validatorSchema['file'] = new sfValidatorFile(array(
		    'required' => false,
			'max_size' => '5242880',
			'path' => sfConfig::get('sf_upload_dir'),
			'validated_file_class' => 'sfValidatedPicture',
			'mime_types' => 'web_images'),
			array('max_size' => 'El tamaño máximo es de 5MB',
				'mime_types' => 'Sólo se permiten imágenes jpg, png o gif'));
	$this->setValidator('description', new sfValidatorString(array('required' => false)));

	$this->widgetSchema->setNameFormat('file[%s]');
	$this->validatorSchema->setOption('allow_extra_fields', true);
  }

  /*
  protected function doSave ( $con = null )
  {
	  $upload = $this->getValue('file');
	  if ( $upload )
	  {
		  $filename = sha1($upload->getOriginalName().microtime().rand()).$upload->getExtension($upload->getOriginalExtension());
		  $filepath = sfConfig::get('sf_upload_dir').'/'.$filename;
		  $thumbnailpath = sfConfig::get('sf_thumbnail_dir').'/'.$filename;

		  if ($this->getObject()->getFile() && $this->getObject()->getFile() != ""){
			  $oldfilepath = sfConfig::get('sf_upload_dir').'/'.$$this->getObject()->getFile();

			  if (file_exists($oldfilepath) )
			  {
				  unlink($oldfilepath);
			  }

			  $oldthumbnailpath = sfConfig::get('sf_thumbnail_dir').'/'.$this->getObject()->getFile();
			  if ( file_exists($oldthumbnailpath) )
			  {
				  unlink($oldthumbnailpath);
			  }
		  }

		  $upload->save($filepath);

		  $thumbnail = new sfThumbnail(90, 90, true, true, 75, 'sfGDAdapter');
		  $thumbnail->loadFile($filepath);
		  $thumbnail->save($thumbnailpath);
	  }

	  $delete = $this->getValue('file_delete');
	  if ( $delete )
	  {
		  $filename = $this->getObject()->getFile();
		  $filepath = sfConfig::get('sf_upload_dir').'/'.$filename;
		  @unlink($filepath);
		  $this->getObject()->delete();

		  $thumbnailpath = sfConfig::get('sf_thumbnail_dir').'/'.$filename;
		  @unlink($thumbnailpath);

	  }

	  return parent::doSave($con);
  }

  public function updateObject($values = null)
  {
    $object = parent::updateObject($values);
    $object->setFile(str_replace(sfConfig::get('sf_upload_dir').'/', '', $object->getFile()));
    return $object;
  }

  public static function formValuesAreBlank(array $values)
  {
    $fieldNames = array_diff(Doctrine::getTable('ProductPicture')->getFieldNames(), array(
      'id',
      'product_id',
    ));

    foreach ($fieldNames as $fieldName)
    {
      if (isset($values[$fieldName]) && !self::formValueIsBlank($values[$fieldName])) return false;
    }

    return true;
  }


  public static function formValueIsBlank($value)
  {
    if (is_array($value))
    {
      foreach ($value as $subValue)
      {
        if (!self::formValueIsBlank($subValue)) return false;
      }

      return true;
    }

    return $value ? false : true;
  }
  */
}
